//
//  HD_NKM_RootVC.m
//  HDLiaoNingMeseum
//
//  Created by HDNiuKuiming on 2017/8/3.
//  Copyright © 2017年 HDNiuKuiming. All rights reserved.
//

#import "HD_NKM_RootVC.h"
#import "HD_NKM_RootVC+Tableview.h"
#import "HD_HSP_FirstDataViewController.h"
#import "HD_LY_DownloadManager.h"
#import "HD_HSP_Friends_ViewController.h"
#import "HD_HSP_AQcode_ViewController.h"
#import "HD_HSP_Messages_ViewController.h"
#import "HD_HSP_GL_ViewController.h"
#import "HD_HSP_Help_ViewController.h"
#import "HD_HSP_Setting_ViewController.h"
#import "HD_HSP_Search_ViewController.h"
#import "HD_HSP_Boutique_ViewController.h"
//
static HDDeclare *declare;
@interface HD_NKM_RootVC ()
@property (nonatomic, strong) NSArray *btnImgArray;
@property (weak, nonatomic) IBOutlet UIButton *cellOneBtn;
@property (weak, nonatomic) IBOutlet UIButton *cellTwoBtn;
@property (weak, nonatomic) IBOutlet UIButton *cellThreeBtn;
@property (weak, nonatomic) IBOutlet UIButton *cellFourBtn;

@property(strong,nonatomic)HD_LY_DownloadManager *downloadManager;
@end

@implementation HD_NKM_RootVC

#pragma mark
#pragma mark -------- Init Methods --------

- (void)viewDidLoad {
    [super viewDidLoad];
    
    declare = [HDDeclare sharedDeclare];

    [self datasInitAction];
    [self labelTitlesInit];

    [self reloadTableView];
    
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(showMessageAlertWith:) name:kSystemMessageNoti object:nil];
    
//    [self insertOneNewInTable];
    
    if (![[NSFileManager defaultManager]fileExistsAtPath:[NSString stringWithFormat:@"%@/%@",declare.catch_path,declare.language.uppercaseString]]) {
        [self tipDownloadAction];
    }
}

-(void)showMessageAlertWith:(NSNotification*)noti
{
    NSDictionary *dic = [noti object];
    HD_HSP_Broadcast *news = [[HD_HSP_Broadcast alloc] init];
    news.identifier = [dic objectForKey:@"zixun_id"];
    news.title = [dic objectForKey:@"title"];
    news.time = [dic objectForKey:@"time"];
    news.content = [dic objectForKey:@"desc"];
    news.html_path = [dic objectForKey:@"html_path"];
    news.list_img = [dic objectForKey:@"list_img"];
}

-(void)insertOneNewInTable
{
    HD_HSP_Broadcast *model = [[HD_HSP_Broadcast alloc]init];
    model.identifier = @"002";
    model.title = @"乐标题";
    model.time = @"2018";
    model.content = @"比较二的内容";
    model.status = @"new";
    
    [declare addANewWith:model];
}


- (void)datasInitAction
{
    _cellImageArray = @[@"root_cell_A_img",@"root_cell_B_img",@"root_cell_C_img",@"root_cell_D_img",@"root_cell_E_img"];
    _btnImgArray = @[@"root_top_btn_A",@"root_top_btn_C",@"root_top_btn_D",@"root_top_btn_E",@"root_top_btn_B"];
}

- (void)labelTitlesInit
{
    NSInteger btnNum = 5;
    if (_btnImgArray.count < btnNum)
        return;
    
    NSString *btnSuffix = @"";
    if ([[HDDeclare sharedDeclare] languageIndex] == kLanguage_Type_English)
    {
        btnSuffix = @"_E";
    }
    else if ([[HDDeclare sharedDeclare] languageIndex] == kLanguage_Type_Japanese)
    {
        btnSuffix = @"_J";
    }
    else if ([[HDDeclare sharedDeclare] languageIndex] == kLanguage_Type_Korean)
    {
        btnSuffix = @"_K";
    }
    
    NSString *imgAName = [NSString stringWithFormat:@"%@%@", _btnImgArray[0],btnSuffix];
    NSString *imgBName = [NSString stringWithFormat:@"%@%@", _btnImgArray[1],btnSuffix];
    NSString *imgCName = [NSString stringWithFormat:@"%@%@", _btnImgArray[2],btnSuffix];
    NSString *imgDName = [NSString stringWithFormat:@"%@%@", _btnImgArray[3],btnSuffix];
    NSString *imgEName = [NSString stringWithFormat:@"%@%@", _btnImgArray[4],btnSuffix];
    
    [btnA_news setImage:[UIImage imageNamed:imgAName] forState:UIControlStateNormal];
    [btnB_friend setImage:[UIImage imageNamed:imgBName] forState:UIControlStateNormal];
    [btnC_mine setImage:[UIImage imageNamed:imgCName] forState:UIControlStateNormal];
    [btnD_strategy setImage:[UIImage imageNamed:imgDName] forState:UIControlStateNormal];
    [btnE_help setImage:[UIImage imageNamed:imgEName] forState:UIControlStateNormal];
    
    
    NSString *cellAName = [NSString stringWithFormat:@"%@%@", _cellImageArray[0],btnSuffix];
    NSString *cellBName = [NSString stringWithFormat:@"%@%@", _cellImageArray[1],btnSuffix];
    NSString *cellCName = [NSString stringWithFormat:@"%@%@", _cellImageArray[2],btnSuffix];
    NSString *cellDName = [NSString stringWithFormat:@"%@%@", _cellImageArray[3],btnSuffix];
    
    [_cellOneBtn setImage:[UIImage imageNamed:cellAName] forState:UIControlStateNormal];
    [_cellTwoBtn setImage:[UIImage imageNamed:cellBName] forState:UIControlStateNormal];
    [_cellThreeBtn setImage:[UIImage imageNamed:cellCName] forState:UIControlStateNormal];
    [_cellFourBtn setImage:[UIImage imageNamed:cellDName] forState:UIControlStateNormal];
    
    
    
    for (UIButton *btn in self.view.subviews) {
        if ([btn isKindOfClass:[UIButton class]])
            btn.exclusiveTouch = YES;
    }
}

#pragma mark - 懒加载

-(HD_LY_DownloadManager *)downloadManager
{
    if (!_downloadManager) {
        _downloadManager = [[HD_LY_DownloadManager alloc]init];
    }
    return _downloadManager;
}


#pragma mark - 点击事件

- (IBAction)settingClicked:(id)sender {
    HD_HSP_Setting_ViewController *firVC = [[UIStoryboard storyboardWithName:@"HD_HSP_Setting" bundle:nil] instantiateViewControllerWithIdentifier:@"HD_HSP_Setting_ViewController"];
    [self.navigationController pushViewController:firVC animated:YES];
}



- (IBAction)helpClicked:(id)sender {
    HD_HSP_Help_ViewController *firVC = [[UIStoryboard storyboardWithName:@"HD_HSP_Help" bundle:nil] instantiateViewControllerWithIdentifier:@"HD_HSP_Help_ViewController"];
    [self.navigationController pushViewController:firVC animated:YES];
}



- (IBAction)shuZiDianBoClicked:(id)sender {
    HD_HSP_Search_ViewController *firVC = [[UIStoryboard storyboardWithName:@"HD_HSP_Search" bundle:nil] instantiateViewControllerWithIdentifier:@"HD_HSP_Search_ViewController"];
    [self.navigationController pushViewController:firVC animated:YES];
}


- (IBAction)gongLueClicked:(id)sender {
    HD_HSP_GL_ViewController *firVC = [[UIStoryboard storyboardWithName:@"HD_HSP_Strategy" bundle:nil] instantiateViewControllerWithIdentifier:@"HD_HSP_GL_ViewController"];
    [self.navigationController pushViewController:firVC animated:YES];
}


- (IBAction)checkNewsClicked:(id)sender {
    HD_HSP_Messages_ViewController *firVC = [[UIStoryboard storyboardWithName:@"HD_Messages" bundle:nil] instantiateViewControllerWithIdentifier:@"HD_HSP_Messages_ViewController"];
    [self.navigationController pushViewController:firVC animated:YES];
}


- (IBAction)firendsClicked:(id)sender {
    HD_HSP_FirstDataViewController *firVC = [[UIStoryboard storyboardWithName:@"HD_HSP_MineCenter" bundle:nil] instantiateViewControllerWithIdentifier:@"HD_HSP_MineSelect_ViewController"];
    [self.navigationController pushViewController:firVC animated:YES];
}



- (IBAction)mapClicked:(id)sender {
    HD_HSP_FirstDataViewController *firVC = [[UIStoryboard storyboardWithName:@"HD_HSP_Map" bundle:nil] instantiateViewControllerWithIdentifier:@"HD_HSP_Map_ViewController"];
    [self.navigationController pushViewController:firVC animated:YES];
}



- (IBAction)backClick:(id)sender {
//    [self.navigationController popViewControllerAnimated:YES];
    
    
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"HD_AQcode" bundle:nil];
    HD_HSP_AQcode_ViewController *vc = [board instantiateViewControllerWithIdentifier:@"HD_HSP_AQcode_ViewController"];
    [self.navigationController pushViewController:vc animated:YES];
}


- (IBAction)dataClicked:(id)sender {
    HD_HSP_FirstDataViewController *firVC = [[UIStoryboard storyboardWithName:@"HD_HSP_Data" bundle:nil] instantiateViewControllerWithIdentifier:@"HD_HSP_FirstDataViewController"];
    [self.navigationController pushViewController:firVC animated:YES];
}

- (IBAction)jingPinClicked:(id)sender {
    
    HD_HSP_Boutique_ViewController *firVC = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"HD_HSP_Boutique_ViewController"];
    [self.navigationController pushViewController:firVC animated:YES];
    
    
    
}

-(void)downloadAllResource
{
    [self.downloadManager myDownloadAction:self.navigationController.visibleViewController withType:kDownload_Tpye_AllRes WithFileNo:nil];
    self.downloadManager.successBlock = ^{
        NSLog(@"AllRes下载完成....");
    };
}

#pragma mark
#pragma mark -------- Tip Methods --------

- (void)tipDownloadAction
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:declare.alertTitle message:declare.allResourceTips preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:declare.alertOK style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self downloadAllResource];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:declare.alertCancel style:UIAlertActionStyleDefault handler:NULL];
    [alertVC addAction:okAction];
    [alertVC addAction:cancelAction];
    
    [self presentViewController:alertVC animated:YES completion:NULL];
}

#pragma mark
#pragma mark -------- Dealloc Methods --------

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
